<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="订单编号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="司机姓名" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入司机姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="保险id" prop="insureId">
        <el-input
          v-model="queryParams.insureId"
          placeholder="请输入保险接口返回保险id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in stateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

    </el-row>

    <el-table v-loading="loading" :data="insureList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单编号" align="center" prop="orderNum"/>
      <el-table-column label="司机姓名" align="center" prop="driverName"/>
      <el-table-column label="会员手机号" align="center" prop="memberMobile"/>
      <el-table-column label="司机手机号" align="center" prop="driverMobile"/>
      <el-table-column label="订单起点" align="center" prop="startLoc"/>
      <el-table-column label="订单终点" align="center" prop="endLoc"/>
      <!--      <el-table-column label="平台分配公司名称" align="center" prop="company" />-->
      <el-table-column label="订单距离" align="center" prop="distance"/>
      <el-table-column label="订单完成时间" align="center" prop="orderFinishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderFinishTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接单时间" align="center" prop="driverPickTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.driverPickTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保险id" align="center" prop="insureId"/>
      <el-table-column label="状态" align="center" prop="state" :formatter="stateFormat"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReOrder(scope.row)"
            v-if="scope.row.state === 1  || scope.row.state === 10"
            v-hasPermi="['order:insure:queryAndReOrder']"
          >查询/重试
          </el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleQueryAndReOrder(scope.row)"
            v-if="scope.row.state === 4  || scope.row.state === 13"
            v-hasPermi="['order:insure:reOrder']"
          >重试
          </el-button>


          <el-button
            size="mini"
            type="text"
            icon="el-icon-map-location"
            @click="handPoint(scope.row)"
            v-hasPermi="['order:insure:reOrder']"
          >轨迹
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-drawer
      title="轨迹"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
      size="50%">
      <point :polyArr="polyArr" :centerPoint="centerPoint" :key="(new Date().getTime())"></point>
    </el-drawer>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
  import {
    listInsure,
    getInsure,
    addInsure,
    updateInsure,
    reOrder,
    queryAndReOrder,
    loadpoint
  } from "@/api/order/insure";
  import Point from './point';
  export default {
    name: "OrderInsure",
    components: {Point},
    data() {
      return {
        drawer: false,
        direction: 'rtl',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 订单保险表格数据
        insureList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态字典
        stateOptions: [],
        orderId: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderId: undefined,
          orderSn: undefined,
          driverName: undefined,
          memberMobie: undefined,
          driverMobile: undefined,
          startLoc: undefined,
          endLoc: undefined,
          company: undefined,
          distance: undefined,
          orderFinishTime: undefined,
          driverPickTime: undefined,
          insureId: undefined,
          state: undefined,
          gmtTime: undefined,
        },
        // 表单参数
        form: {},

        // polyArr: [[121.5389385, 31.21515044], [121.5389385, 31.29615044], [121.5273285, 31.21515044]]
        polyArr: [],
        centerPoint: []
      };
    },
    created() {
      this.getList();
      this.getDicts("insure_state").then(response => {
        this.stateOptions = response.data;
      });
    },
    methods: {
      /** 查询订单保险列表 */
      getList() {
        this.loading = true;
        listInsure(this.queryParams).then(response => {
          this.insureList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      stateFormat(row, column) {
        return this.selectDictLabel(this.stateOptions, row.state);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          orderId: undefined,
          orderSn: undefined,
          driverName: undefined,
          memberMobie: undefined,
          driverMobile: undefined,
          startLoc: undefined,
          endLoc: undefined,
          company: undefined,
          distance: undefined,
          orderFinishTime: undefined,
          driverPickTime: undefined,
          insureId: undefined,
          state: undefined,
          remark: undefined,
          gmtTime: undefined,
          updateTime: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加订单保险";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getInsure(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改订单保险";
        });
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateInsure(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addInsure(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleQueryAndReOrder(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认查询订单保险编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return queryAndReOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        }).catch(function () {
        });
      },

      /** 删除按钮操作 */
      handleReOrder(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认重试订单保险编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return reOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("操作成功");
        }).catch(function () {
        });
      },
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {
          });
      },
      handPoint(row) {
        this.orderId = row.orderId;
        this.polyArr = []
        this.getPointList();

      },
      getPointList() {
        loadpoint(this.orderId).then(res => {

          let cenArr = res.data.stationLoc.split(",");
          this.centerPoint[0] = parseFloat(cenArr[0])
          this.centerPoint[1] = parseFloat(cenArr[1])

          res.data.points.forEach(item => {
            let arr = [];
            let coor = item.split(",");
            arr[0] = parseFloat(coor[0]);
            arr[1] = parseFloat(coor[1]);
            this.polyArr.push(arr)
          })
          this.drawer = true;
        });
      }
    }
  };
</script>
